#include "stdafx.h"
#include "CharsetConvert.h"


CString ca2ct(const char* buf)  
{  
	int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);  
	if (len == 0) return _T("");  

	std::vector<TCHAR> unicode(len);  
	::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);  

	return &unicode[0];  
}  

std::string ct2ca(const wchar_t* buf)   
{  
	int len = ::WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);  
	if (len == 0) return "";  

	std::vector<char> ascii(len);  
	::WideCharToMultiByte(CP_ACP, 0, buf, -1, &ascii[0], len, NULL, NULL);  

	return &ascii[0];  
}  

CString cu2ct(const char* buf)  
{  
	int len = ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);  
	if (len == 0) return _T("");  

	std::vector<TCHAR> unicode(len);  
	::MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);  

	return &unicode[0];  
}  

std::string ct2cu(const wchar_t* buf)  
{  
	int len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);  
	if (len == 0) return "";  

	std::vector<char> utf8(len);  
	::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);  

	return &utf8[0];  
}
